package com.yt.order.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Calendar;
import java.util.Date;
import com.yt.order.models.service.IBillService;
import com.yt.order.models.bo.BillSaveBO;
import com.yt.order.models.bo.BillBO;
import com.yt.order.models.dto.BillDTO;

/**
 * 账单信息控制器
 *
 * @author yzz
 * @Date 2025-10-22 09:41:05
 */

@RestController
@RequestMapping("/cms/bill")
@Slf4j
@Api(value = "BillController",tags = "账单信息接口")
public class BillController {

    @DubboReference
    private IBillService billService;

    @ApiOperation(value = "添加账单信息",notes = "添加账单信息")
    @PostMapping(value = "/insert")
    public Result insert(@RequestBody BillSaveBO saveBO) {
        boolean flag = billService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存账单信息",notes = "保存账单信息")
    public Result save(@RequestBody BillSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改账单信息",notes = "修改账单信息")
    @PostMapping("/update")
    public Result update(@RequestBody BillSaveBO saveBO){
        BillDTO dto = new BillDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(Long.valueOf(saveBO.getId()));
        boolean flag = billService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除账单信息",notes = "删除账单信息")
    @ApiImplicitParam(name = "id",value = "账单信息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = billService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除账单信息",notes = "批量删除账单信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        billService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<BillDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        BillDTO dto = billService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "账单信息列表",notes = "账单信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<BillDTO>> queryPageList(BillBO bo){
        Wrapper<BillDTO> wrapper = new EntityWrapper<>();


        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        
        // 确保分页参数有默认值
        if (bo.getPageNo() == null || bo.getPageNo() <= 0) {
            bo.setPageNo(1);
        }
        if (bo.getPageSize() == null || bo.getPageSize() <= 0) {
            bo.setPageSize(10);
        }
        
        Page<BillDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = billService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @ApiOperation(value = "账单信息列表",notes = "账单信息列表")
    @PostMapping("/listAll")
    public Result<List<BillDTO>> listAll(BillBO bo){
        Wrapper<BillDTO> wrapper = new EntityWrapper<>();
        List<BillDTO> list = billService.selectList(wrapper);
        return new Result<>(list);
    }
    
    @ApiOperation(value = "根据年月筛选账单信息", notes = "根据年月筛选账单信息")
    @PostMapping("/queryByYearMonth")
    public Result<List<BillDTO>> queryByYearMonth(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month) {
        
        Wrapper<BillDTO> wrapper = new EntityWrapper<>();
        
        // 如果年份和月份都提供了，则构造日期范围
        if (year != null && month != null) {
            // 构造开始时间 (月份的第一天 00:00:00)
            Calendar startCal = Calendar.getInstance();
            startCal.set(year, month - 1, 1, 0, 0, 0);
            startCal.set(Calendar.MILLISECOND, 0);
            Date startDate = startCal.getTime();
            
            // 构造结束时间 (月份的最后一天 23:59:59)
            Calendar endCal = Calendar.getInstance();
            endCal.set(year, month - 1, 1, 23, 59, 59);
            endCal.set(Calendar.MILLISECOND, 999);
            endCal.add(Calendar.MONTH, 1);
            endCal.add(Calendar.DAY_OF_MONTH, -1);
            Date endDate = endCal.getTime();
            
            wrapper.ge("create_time", startDate);
            wrapper.le("create_time", endDate);
        } else if (year != null) {
            // 只提供了年份，筛选整年数据
            Calendar startCal = Calendar.getInstance();
            startCal.set(year, 0, 1, 0, 0, 0); // 该年1月1日 00:00:00
            startCal.set(Calendar.MILLISECOND, 0);
            Date startDate = startCal.getTime();
            
            Calendar endCal = Calendar.getInstance();
            endCal.set(year, 11, 31, 23, 59, 59); // 该年12月31日 23:59:59
            endCal.set(Calendar.MILLISECOND, 999);
            Date endDate = endCal.getTime();
            
            wrapper.ge("create_time", startDate);
            wrapper.le("create_time", endDate);
        }
        
        List<BillDTO> list = billService.selectList(wrapper);

        return new Result<>(list);
    }
}